﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Xml.Linq;
using Simulator.Properties;
using STP;

namespace Simulator
{
	public partial class NewBridgeForm : Form
	{
		static int _nextUniqueId = 1;
		static ulong _nextMacAddress = 0x025555aaaa01;

		static readonly Array _protocolValues = Enum.GetValues (typeof (StpVersion));

		readonly SheetArea _sheetArea;
		Bridge _newBridge;

		public NewBridgeForm (SheetArea sheetArea)
		{
			_sheetArea = sheetArea;

			InitializeComponent ();

			// For proper scaling, we must set the font _after_ calling InitializeComponent.
			var font = new Font ("Segoe UI", 9);
			if (font.OriginalFontName != font.Name)
			{
				// Requested font name doesn't exist on the system. We're probably on Windows XP. Let's fallback to Tahoma.
				font.Dispose ();
				font = new Font ("Tahoma", 9);
			}
			this.Font = font;

			foreach (var p in _protocolValues)
				comboBoxProtocol.Items.Add (p);
		}

		protected override void OnShown (EventArgs e)
		{
			base.OnShown (e);

			textBoxMacAddress.Text = BridgeAddressTypeConverter.ConvertToString (_nextMacAddress, BridgeAddressTypeConverter.ConvertToStringOptions.SeparatorColon);
			textBoxDebugName.Text = String.Format ("Bridge {0}", _nextUniqueId);

			numericUpDownPortCount.Value = Settings.Default.NewBridgePortCount;
			comboBoxProtocol.SelectedIndex = Settings.Default.NewBridgeProtocol;
			numericUpDownTreeCount.Value = Settings.Default.NewBridgeTreeCount;
		}

		protected override void OnFormClosing (FormClosingEventArgs e)
		{
			base.OnFormClosing (e);

			if (e.Cancel)
				return;

			if (this.DialogResult != DialogResult.OK)
				return;

			ulong macAddress;
			try
			{
				macAddress = BridgeAddressTypeConverter.ConvertFromString (textBoxMacAddress.Text);
			}
			catch (Exception ex)
			{
				MessageBox.Show (this, ex.Message, "Invalid bridge MAC address");
				textBoxMacAddress.Focus ();
				e.Cancel = true;
				return;
			}

			StpVersion version = (StpVersion) Enum.Parse (typeof (StpVersion), comboBoxProtocol.SelectedItem.ToString ());
			if (version == StpVersion.LegacySTP)
			{
				MessageBox.Show (this, "Legacy STP is not currently supported by the library. Choose RSTP or MSTP.");
				comboBoxProtocol.Focus ();
				e.Cancel = true;
				return;
			}

			uint treeCount = (version < StpVersion.MSTP) ? 1 : (uint) numericUpDownTreeCount.Value;

			uint portCount = (uint) numericUpDownPortCount.Value;
			_newBridge = new Bridge (_sheetArea, portCount, treeCount, version, macAddress, _nextUniqueId, textBoxDebugName.Text);

			_nextUniqueId++;
			_nextMacAddress = macAddress + 1;
			if ((_nextMacAddress & 0xff) == 0)
				_nextMacAddress++;

			Settings.Default.NewBridgePortCount = numericUpDownPortCount.Value;
			Settings.Default.NewBridgeProtocol = comboBoxProtocol.SelectedIndex;
			Settings.Default.NewBridgeTreeCount = numericUpDownTreeCount.Value;
			Settings.Default.Save ();
		}

		public Bridge NewBridge
		{
			[DebuggerStepThrough]
			get { return _newBridge; }
		}

		private void comboBoxProtocol_SelectedIndexChanged (object sender, EventArgs e)
		{
			StpVersion version = (StpVersion) Enum.Parse (typeof (StpVersion), comboBoxProtocol.SelectedItem.ToString ());
			numericUpDownTreeCount.Enabled = (version >= StpVersion.MSTP);
		}
	}
}
